package com.awspaas.user.apps.shhtaerospaceindustrial.apartmanage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.sms.SmsUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class CheckinApplySendMessage extends ExecuteListener{

	public String getDescription() {
		return "新办理入住审批结束，给入住人发短信消息及更新入住信息。";
	}
	
	@Override
	public void execute(ProcessExecutionContext param) throws Exception {
		boolean flag_ok = SDK.getTaskAPI().isChoiceActionMenu(param.getTaskInstance(), "同意");
		if(flag_ok) {
			//获取流程实例ID
			String processInstId = param.getProcessInstance().getId();
			//根据流程实例ID获取任务表内数据
			List<Map<String , Object>> headlist = DBSql.query("SELECT * FROM BO_EU_SH_CHECKIN  WHERE BINDID = ? ", 
					new ColumnMapRowMapper(), new Object[] {processInstId});
			List<Map<String , Object>> bodylist = DBSql.query("SELECT * FROM BO_EU_SH_CHECKIN_B  WHERE BINDID = ? ", 
					new ColumnMapRowMapper(), new Object[] {processInstId});
			Map<String , Object>  jcbd = headlist.get(0);
			String ID=CoreUtil.objToStr(jcbd.get("ID"));//ID
//			String UNITNUM = CoreUtil.objToStr(jcbd.get("UNITNUM"));//单元
//			String ACCOUNTNUM=CoreUtil.objToStr(jcbd.get("ACCOUNTNUM"));//室号
			String CHECKINTIME=CoreUtil.objToStr(jcbd.get("CHECKINTIME"));//入住时间
			String RENTBEGINTIME=CoreUtil.objToStr(jcbd.get("RENTBEGINTIME"));//租用开始时间
			String RENTENDTIME=CoreUtil.objToStr(jcbd.get("RENTENDTIME"));//租用结束时间
			String WORKERSNAME=CoreUtil.objToStr(jcbd.get("WORKERSNAME"));//职工姓名
			String SEX=CoreUtil.objToStr(jcbd.get("SEX"));//性别
			String NATION=CoreUtil.objToStr(jcbd.get("NATION"));//民族
			String EDUCATION=CoreUtil.objToStr(jcbd.get("EDUCATION"));//学历
			String BIRTHDATE=CoreUtil.objToStr(jcbd.get("BIRTHDATE"));//出生日期
			String TELEPHONE=CoreUtil.objToStr(jcbd.get("TELEPHONE"));//联系电话
			String IDCARD=CoreUtil.objToStr(jcbd.get("IDCARD"));//身份证号
			String DOMICILEADDERSS=CoreUtil.objToStr(jcbd.get("DOMICILEADDERSS"));//户籍地址
			String EMERGENTNAME=CoreUtil.objToStr(jcbd.get("EMERGENTNAME"));//紧急联系人
			String EMERGENTTELE=CoreUtil.objToStr(jcbd.get("EMERGENTTELE"));//紧急联系人电话号
			String ANNEX=CoreUtil.objToStr(jcbd.get("ANNEX"));//租赁合同附件
			String ACCOUNTID=CoreUtil.objToStr(jcbd.get("ACCOUNTID"));//住宅号id
			String APPLYUNIT=CoreUtil.objToStr(jcbd.get("APPLYUNIT"));//申请单位
			List<BO> updatahead = new ArrayList<BO>();
			BO zghead = new BO();
			zghead.set("PSNTYPE","航天职工");//人员类别
			zghead.set("PSNNAME",WORKERSNAME);//姓名
			zghead.set("SEX",SEX);//性别
			zghead.set("BIRTHDATE",BIRTHDATE);//出生日期
			zghead.set("EDUCATION",EDUCATION);//学历
			zghead.set("NATION",NATION);//民族
			zghead.set("PSNCARDNO",IDCARD);//身份证号码
			zghead.set("DOMICILEADDERSS",DOMICILEADDERSS);//户籍地址
			zghead.set("PSNPHONE",TELEPHONE);//手机号码
			zghead.set("EMERGENTNAME",EMERGENTNAME);//紧急联系人
			zghead.set("EMERGENTTELE",EMERGENTTELE);//紧急联系人电话号
			zghead.set("CHECKINTIME",CHECKINTIME);//入住时间
			zghead.set("RENTBEGINTIME",RENTBEGINTIME);//租用开始时间
			zghead.set("RENTENDTIME",RENTENDTIME);//租用结束时间
			zghead.set("YUANGONGZH",ID);//员工账号,新员工未建账号，先填充入住表ID
			updatahead.add(zghead);
			if(!"".equals(ACCOUNTID)) {
				String sqlzlb = "select bindid from BO_EU_SH_RCGY_ROOM t where  t.id = '"
						+ ACCOUNTID + "'";
				String cardbindid = CoreUtil.objToStr(DBSql.getString(sqlzlb, "bindid"));// bindid
				//插入入住人员数据
				SDK.getBOAPI().create("BO_EU_SH_RCGY_ROOM_PSN", updatahead, cardbindid, "admin");
				if(!"".equals(ANNEX)) {
					String sql = "select id from BO_EU_SH_RCGY_ROOM_PSN t where  t.YUANGONGZH = '"
							+ ID + "'";
					String sql1 = "select bindid from BO_EU_SH_RCGY_ROOM_PSN t where  t.YUANGONGZH = '"
							+ ID + "'";
					String cardid = CoreUtil.objToStr(DBSql.getString(sql, "id"));// id
					String bindid = CoreUtil.objToStr(DBSql.getString(sql1, "bindid"));// bindid
					SDK.getBOAPI().copyFileTo(ID, "ANNEX", cardid, "BO_EU_SH_RCGY_ROOM_PSN",
							"ANNEX", bindid, null);
					}
				//插入入住人家属数据
				List<BO> updataall = new ArrayList<BO>();
				if (bodylist != null && bodylist.size() > 0) {
					for (int i = 0; i < bodylist.size(); i++) {
						BO updatabody = new BO();
						Map<String , Object> dataprobleminvent = bodylist.get(i);
						String workersname = CoreUtil.objToStr(dataprobleminvent.get("WORKERSNAME"));// 姓名
						String sex = CoreUtil.objToStr(dataprobleminvent.get("SEX"));// 性别
						String nation = CoreUtil.objToStr(dataprobleminvent.get("NATION"));// 民族
						String birthdate = CoreUtil.objToStr(dataprobleminvent.get("BIRTHDATE"));// 出生日期
						String idcard = CoreUtil.objToStr(dataprobleminvent.get("IDCARD"));// 身份证号
						String domicileadderss = CoreUtil.objToStr(dataprobleminvent.get("DOMICILEADDERSS"));// 户籍地址
						String telephone = CoreUtil.objToStr(dataprobleminvent.get("TELEPHONE"));// 联系电话
						String persontype = CoreUtil.objToStr(dataprobleminvent.get("PERSONTYPE"));// 人员类别
						updatabody.set("PSNTYPE",persontype);//人员类别
						updatabody.set("PSNNAME",workersname);//姓名
						updatabody.set("SEX",sex);//性别
						updatabody.set("BIRTHDATE",birthdate);//出生日期
//						updatabody.set("EDUCATION",EDUCATION);//学历
						updatabody.set("NATION",nation);//民族
						updatabody.set("PSNCARDNO",idcard);//身份证号码
						updatabody.set("DOMICILEADDERSS",domicileadderss);//户籍地址
						updatabody.set("PSNPHONE",telephone);//手机号码
//						updatabody.set("EMERGENTNAME",EMERGENTNAME);//紧急联系人
//						updatabody.set("EMERGENTTELE",EMERGENTTELE);//紧急联系人电话号
						updatabody.set("CHECKINTIME",CHECKINTIME);//入住时间
//						updatabody.set("RENTBEGINTIME",RENTBEGINTIME);//租用开始时间
//						updatabody.set("RENTENDTIME",RENTENDTIME);//租用结束时间
						updatabody.set("YUANGONGZH",ID);//员工账号,新员工未建账号，先填充入住表ID
						updataall.add(updatabody);
					}
					SDK.getBOAPI().create("BO_EU_SH_RCGY_ROOM_PSN", updataall, cardbindid, "admin");
				}
				
			}
			//更新入住人数字段
			String sqlbindid = "select bindid from BO_EU_SH_RCGY_ROOM_PSN t where  t.YUANGONGZH = '"
					+ ID + "'";
			String bodybindid = CoreUtil.objToStr(DBSql.getString(sqlbindid, "bindid"));// bindid
			String queryUrl ="SELECT ID FROM BO_EU_SH_RCGY_ROOM_PSN  where bindid ='"+bodybindid+"'";
				List<Map<String, Object>> queryUrlList = DBSql.query(queryUrl, new ColumnMapRowMapper(), new Object[]{});
			if(queryUrlList ==null ||queryUrlList.isEmpty()) {
				String updatenosql = "update  BO_EU_SH_RCGY_ROOM set INROOMNUM = '0' where id ='" + ACCOUNTID + "'";
				DBSql.update(updatenosql);
			}else {
				String updatenosql = "update  BO_EU_SH_RCGY_ROOM set INROOMNUM = '"+queryUrlList.size()+"' where id ='" + ACCOUNTID + "'";
				DBSql.update(updatenosql);
			}
			//短信通知办理入住
			String str = "{'APPLYUNIT':'" + APPLYUNIT + "'}";// 总的车牌信息
			JSONObject result=SmsUtil.sendSms(TELEPHONE, "SMS_463930499", str);// 新员工入住人才公寓办理手续
			System.out.println("result is" + result);
		}
			
	}

}
